package com.guchenbo.erp;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.ConstVal;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.TemplateType;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.guchenbo.erp.dao.entity.ErpBaseModel;

import java.sql.Types;
import java.util.Collections;

/**
 * @author guchenbo
 * @date 2024/4/7
 */
public class DaoGenerator {
    public static void main(String[] args) {
        /*
        https://baomidou.com/pages/981406
         */
        String moduleName = "inventory";
        String moduleDir = "gcb-erp-" + moduleName;
        String database = "gcb_erp_" + moduleName;

        String url = "jdbc:mysql://localhost:3306/" + database + "?useSSL=false&allowPublicKeyRetrieval=true";
        String username = "gcb";
        String password = "guchenbo";
        FastAutoGenerator.create(url, username, password).globalConfig(builder -> {
                            builder.author("guchenbo") // 设置作者
                                            .fileOverride() // 覆盖已生成文件
                                            // 指定输出目录
                                            .outputDir("/Users/guchenbo/projects/personal/gitee/gucb/gcb-erp/"
                                                            + moduleDir
                                                            + "/src/main/java").disableOpenDir();
                        }).dataSourceConfig(builder -> builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
                            int typeCode = metaInfo.getJdbcType().TYPE_CODE;
                            if (typeCode == Types.SMALLINT) {
                                // 自定义类型转换
                                return DbColumnType.INTEGER;
                            }
                            return typeRegistry.getColumnType(metaInfo);

                        })).packageConfig(builder -> {
                            builder.parent("com.guchenbo.erp") // 设置父包名
                                            .moduleName(moduleName) // 设置父包模块名
                                            .pathInfo(Collections.singletonMap(OutputFile.xml,
                                                            "/Users/guchenbo/projects/personal/gitee/gucb/gcb-erp/"
                                                                            + moduleDir
                                                                            + "/src/main/resources/mapper")); // 设置mapperXml生成路径
                        }).strategyConfig(builder -> {
                            // 设置需要生成的表名
//                            builder.addInclude("erp_product");
//                                                        .addTablePrefix("tbl_simba_os_");
                            builder.entityBuilder().disableSerialVersionUID().enableFileOverride().superClass(ErpBaseModel.class)
                                            .addSuperEntityColumns("create_time", "modify_time", "create_user", "modify_user",
                                                            "deleted");
                            builder.mapperBuilder().enableFileOverride().enableBaseResultMap().enableBaseColumnList();
                            builder.addTablePrefix("erp_");
                            builder.serviceBuilder().convertServiceFileName((entityName -> entityName + ConstVal.SERVICE));
                        }).templateConfig(builder -> {
                            builder.disable(TemplateType.CONTROLLER);
                        }).templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                        .execute();
    }
}
